From 1852a32c38cc2ff4baa1fae375d37d86f6b9dc5b Mon Sep 17 00:00:00 2001 From: robertl Date: Wed, 19 Oct 2005 15:18:05 +0000 Subject: [PATCH] Ignore kml, Document, Folder tags to improve read compaitiblity for KMLv2. Also read MultiGeometry/LineStrings as tags. --- gpsbabel/kml.c | 52 ++++++++++++++++++++++++++++++++++++------- gpsbabel/xmlgeneric.c | 34 ++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 8 deletions(-) diff --git a/gpsbabel/kml.c b/gpsbabel/kml.c index 55d7ddeb3..bce4b5c00 100644 --- a/gpsbabel/kml.c +++ b/gpsbabel/kml.c @@ -35,6 +35,7 @@ static int export_points; static int floating; static waypoint *wpt_tmp; +static int wpt_tmp_queued; static FILE *fd; static FILE *ofd; @@ -85,27 +86,39 @@ kml_read(void) #else static xg_callback wpt_s, wpt_e; -static xg_callback wpt_name, wpt_desc, wpt_coord; +static xg_callback wpt_name, wpt_desc, wpt_coord, trk_coord; static xg_tag_mapping kml_map[] = { - { wpt_s, cb_start, "/Document/Folder/Placemark" }, - { wpt_e, cb_end, "/Document/Folder/Placemark" }, - { wpt_name, cb_cdata, "/Document/Folder/Placemark/name" }, - { wpt_desc, cb_cdata, "/Document/Folder/Placemark/description" }, - { wpt_coord, cb_cdata, "/Document/Folder/Placemark/Point/coordinates" }, + { wpt_s, cb_start, "/Placemark" }, + { wpt_e, cb_end, "/Placemark" }, + { wpt_name, cb_cdata, "/Placemark/name" }, + { wpt_desc, cb_cdata, "/Placemark/description" }, + { wpt_coord, cb_cdata, "/Placemark/Point/coordinates" }, + { trk_coord, cb_cdata, "/Placemark/MultiGeometry/LineString/coordinates" }, { NULL, 0, NULL } }; +static +const char * kml_tags_to_ignore[] = { + "kml", + "Document", + "Folder", + NULL, +}; + void wpt_s(const char *args, const char **unused) { wpt_tmp = waypt_new(); -// wpt_tmp = xcalloc(sizeof(*wpt_tmp), 1); + wpt_tmp_queued = 0; } void wpt_e(const char *args, const char **unused) { - waypt_add(wpt_tmp); + if (wpt_tmp_queued) { + waypt_add(wpt_tmp); + } + wpt_tmp_queued = 0; } #if 0 @@ -134,6 +147,28 @@ void wpt_desc(const char *args, const char **unused) void wpt_coord(const char *args, const char **attrv) { sscanf(args, "%lf,%lf,%lf", &wpt_tmp->longitude, &wpt_tmp->latitude, &wpt_tmp->altitude); + wpt_tmp_queued = 1; +} + +void trk_coord(const char *args, const char **attrv) +{ + int consumed = 0; + double lat, lon, alt; + waypoint *trkpt; + + route_head *trk_head = route_head_alloc(); + track_add_head(trk_head); + + while (3 == sscanf(args,"%lf,%lf,%lf %n", &lon, &lat, &alt, &consumed)){ + trkpt = waypt_new(); + trkpt->latitude = lat; + trkpt->longitude = lon; + trkpt->altitude = alt; + + route_add_wpt(trk_head, trkpt); + + args += consumed; + } } static @@ -141,6 +176,7 @@ void kml_rd_init(const char *fname) { xml_init(fname, kml_map, NULL); + xml_ignore_tags(kml_tags_to_ignore); } static diff --git a/gpsbabel/xmlgeneric.c b/gpsbabel/xmlgeneric.c index d23db8d31..d8895634e 100644 --- a/gpsbabel/xmlgeneric.c +++ b/gpsbabel/xmlgeneric.c @@ -32,6 +32,7 @@ static vmem_t current_tag; static vmem_t cdatastr; static FILE *ifd; static xg_tag_mapping *xg_tag_tbl; +static const char **xg_ignore_taglist; #define MY_CBUF 4096 @@ -155,6 +156,27 @@ xml_tbl_lookup(const char *tag, xg_cb_type cb_type) return NULL; } +/* + * See if tag element 't' is in our list of things to ignore. + * Returns 0 if it is not on the list. + */ +static int +xml_consider_ignoring(const char *t) +{ + const char **il; + + if (!xg_ignore_taglist) { + return 0; + } + + for (il = xg_ignore_taglist; *il; il++) { + if (0 == strcmp(*il, t)) { + return 1; + } + } + return 0; +} + static void xml_start(void *data, const char *el, const char **attr) @@ -163,6 +185,9 @@ xml_start(void *data, const char *el, const char **attr) char *ep; xg_callback *cb; + if (xml_consider_ignoring(el)) + return; + vmem_realloc(¤t_tag, strlen(current_tag.mem) + 2 + strlen(el)); e = current_tag.mem; @@ -195,6 +220,9 @@ xml_end(void *data, const char *el) char *s = strrchr(current_tag.mem, '/'); xg_callback *cb; + if (xml_consider_ignoring(el)) + return; + if (strcmp(s + 1, el)) { fprintf(stderr, "Mismatched tag %s\n", el); } @@ -237,6 +265,11 @@ void xml_readstring( char *str ) XML_ParserFree(psr); } +void xml_ignore_tags(const char **taglist) +{ + xg_ignore_taglist = taglist; +} + void xml_init(const char *fname, xg_tag_mapping *tbl, const char *encoding) { @@ -271,6 +304,7 @@ xml_deinit(void) fclose(ifd); ifd = NULL; } + xg_ignore_taglist = NULL; } /******************************************/ -- 2.30.2